package visiopuce.action;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.Action;

import visiopuce.HibernateUtil;
import visiopuce.ICommandIds;
import visiopuce.DAO.ChampDAOImpl;
import visiopuce.objets.Champ;
import visiopuce.objets.Historique;
import visiopuce.service.HistoriqueServiceImpl;
import visiopuce.service.PrelevementServiceImpl;

public class CorrectionChampPrelAction extends Action {

	public CorrectionChampPrelAction(String text) {
		super(text);
		// The id is used to refer to the action in a menu or toolbar
		setId(ICommandIds.CMD_PARSER_LISTE_ADN);
		// Associate the action with a pre-defined command, to allow key
		// bindings.
		setActionDefinitionId(ICommandIds.CMD_PARSER_LISTE_ADN);
		setText("Correction des champs de prélèvement");
		setToolTipText("Correction des champs de prélèvement");
		setImageDescriptor(visiopuce.Activator.getImageDescriptor("/icons/dossierp.png"));
	}

	public void run() {

		List<Historique> histos = HistoriqueServiceImpl.getInstance().getSuppChampObj();
		List<Integer> idPrelevements = new ArrayList<Integer>();
		for (Historique historique : histos) {
			String requete = historique.getRequete();
			String[] tab = requete.split("-");
			if (tab.length > 1) {
				Integer idPrel = Integer.parseInt(tab[1]);
				if (!idPrelevements.contains(idPrel)) {
					idPrelevements.add(idPrel);
				}
			}
		}

		for (Integer idPrelevement : idPrelevements) {
			List<Historique> histosInsert = HistoriqueServiceImpl.getInstance().getHistoInsert(idPrelevement);
			List<String> isSave = new ArrayList<String>();
			for (Historique historique : histosInsert) {
				String requete = historique.getRequete();
				String[] table = requete.split("valeur=");
				String valeur = "";
				if (table.length > 1) {
					valeur = table[1].substring(0, table[1].indexOf(",")).trim();
				}
				String[] tablet = requete.split("type=");
				String type = "";
				if (tablet.length > 1) {
					type = tablet[1].substring(0, tablet[1].indexOf(",")).trim();
				}
				if (valeur.length() > 0 && !valeur.equals("null") && !valeur.equals("")  && !valeur.equals("0") && !isSave.contains("Prel-" + idPrelevement + "-" + type)) {
					Champ champ = PrelevementServiceImpl.getInstance().getChampByTypeAndIdObjet(type, "Prel-" + idPrelevement);
					if (champ == null) {
						Champ c = new Champ();
						c.setIdObjet("Prel-" + idPrelevement);
						c.setType(type);
						c.setValeur(valeur);
						try {
							HibernateUtil.beginTransaction();
							ChampDAOImpl.getInstance().save(c);
							HibernateUtil.commitTransaction();
						} catch (Exception ex) {
							ex.printStackTrace();
							HibernateUtil.rollbackTransaction();
						} finally {
							HibernateUtil.closeSession();
						}
					} else {
						if (champ.getValeur() == null || champ.getValeur().equals("") || champ.getValeur().equals("null") || champ.getValeur().equals("0") ) {
							champ.setValeur(valeur);
							try {
								HibernateUtil.beginTransaction();
								ChampDAOImpl.getInstance().save(champ);
								HibernateUtil.commitTransaction();
							} catch (Exception ex) {
								ex.printStackTrace();
								HibernateUtil.rollbackTransaction();
							} finally {
								HibernateUtil.closeSession();
							}
						}
					}
					isSave.add("Prel-" + idPrelevement + "-" + type);
				}
			}
		}
	}
}